package com.yanqu.road.server.cmd.union.cross;


import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_CONSTRUCTION, desc = "建设联盟")
public class ConstructUnionCmd extends AbstractCommand {

	private int constructType;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionConstructionReqMsg reqMsg = UnionProto.UnionConstructionReqMsg.parseFrom(packet.getBytes());
		constructType = reqMsg.getConstructionType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		String unionUid = player.getUserInfo().getUnionUid();
		if(StringUtils.isNullOrEmpty(unionUid)){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		ret = player.getModule(CrossUnionModule.class).constructUnion(constructType);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionConstructionRespMsg.Builder respMsg = UnionProto.UnionConstructionRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_UNION_CONSTRUCTION, respMsg);
		}
	}

}
